home *** CD-ROM | disk | FTP | other *** search
- Path: news.ifu.net!news
- From: Jason Gresh <gresh@ifu.net>
- Newsgroups: comp.lang.c++
- Subject: Creating a pointer to a function "void (*ptrFunction)()" inside a class
- Date: Thu, 04 Jan 1996 22:54:55 -0500
- Organization: Internet For 'U'
- Message-ID: <30ECA10F.3D99@ifu.net>
- NNTP-Posting-Host: ip21.ifu.net
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 2.0b3 (Win95; I)
-
- Hi,
-
- I am trying to create a base class that has a function that the
- derived class needs to create. In order to do this, I want to create a
- pointer to a function in the base class that the derived class can then
- define. This is not a case where an override will work because the
- number and names of the derived functions is not known. Hopefully this
- code sample will make this clearer:
-
- class BaseClass
- {
- int (*ptrFunction)();
- }
-
- class DerivedClass : BaseClass
- {
- DerivedClass::DerivedClass();
- int myFunction(int, int);
- }
-
- DerivedClass::DerivedClass()
- {
- ptrFunction = myFunction;
- }
-
- DerivedClass::myFunction(int, int)
- {
- // code ....
- }
-
- If I don't make myFunction part of the derived class (global),
- everything works fine. As soon as I include it in the class, I cannot
- assign a pointer to it. I am aware that pointers to functions inside
- the class include the class name in some way ( this is not an issue for
- global functions). What is the casting (or other) mechanism to make
- this work?
-
- Thanks,
-
- Mike Gresh
-